
export default class Proto2Type {
    public static toType(jsonobj: any) {
        let messages = jsonobj["messages"]
        let msarray: Array<string> = new Array();
        for (let k = 0; k < messages.length; k++) {
            let message = messages[k];
            Proto2Type.messageToType(message, msarray);
            //msarray.push(str);
        }
        return msarray.join("\r\n");
    }

    private static messageToType(message: any, msarray: Array<string>, ischild: boolean = false, pername: string = "") {
        let messages = message["messages"];
        if (messages.length > 0) {
            for (let k = 0; k < messages.length; k++) {
                let message_temp = messages[k];
                Proto2Type.messageToType(message_temp, msarray,true,message["name"]);
            }
        }
        let enums= message["enums"];
        if(enums.length>0){
            for(let i=0;i<enums.length;i++){
                let enu=enums[i];
                let enuname=enu["name"]
                let enumobj=new Array();
                enumobj.push(`export enum ${enuname}{`)
                for (let k=0;k<enu["values"].length;k++){
                    let ei= enu["values"][k]
                    let name=ei["name"]
                    let val=ei["id"]
                    let str=` ${name}=${val}${(enu["values"].length==k+1)?"":","}`
                    enumobj.push(str)
                }
                enumobj.push("}")
                msarray.push(enumobj.join(""))
            }
        }


        let messArrayObj: Array<any> = new Array();
        //messArrayObj.push(`type ${ischild ?  pername+"_" : ""}${message["name"]}={`)
        let messageName=Proto2Type.kk(message["name"]);
        messArrayObj.push(`export type ${messageName}={`)
        let fields = message["fields"];
        Proto2Type.messageFiledsToType(fields, messArrayObj, message);
        messArrayObj.push("}")
        msarray.push(messArrayObj.join(""))
        //return messArrayObj.join("")
    }

    private static messageFiledsToType(fields: Array<any>, messArrayObj: Array<any>, message: any) {
        let numberType: Set<string> = new Set();
        numberType.add("double").add("float").add("int32").add("int64").add("uint32").add("uint64").add("sint32").add("sint64").add("fixed32").add("fixed64").add("sfixed32").add("sfixed64");
        for (let j = 0; j < fields.length; j++) {
            let field = fields[j];
            let enu = false;
            let typeName = field["type"]
            if (numberType.has(field["type"])) {
                typeName = "number";
            } else if (field["type"] == "string") {
                typeName = "string"
            } else if (field["type"] == "bool") {
                typeName = "boolean"
            }else{
                let str=typeName
                if(str.lastIndexOf(".")!=-1){
                    typeName= str.substr(str.lastIndexOf(".")+1);
                }
            }
            if(field["rule"]=="repeated"){
                typeName=`Array<${typeName}>`
            }
            let fieldJson = `${field["name"]}:${typeName}${(j == fields.length - 1) ? "" : ","}`
            messArrayObj.push(fieldJson);
        }
    }

    private static kk(str:string):string{
        let strs:Array<string>= str.split("_")
        let sts:Array<string>=new Array()
        strs.forEach((s)=>{
            let b=s.substr(0,1);
            let temp=b.toUpperCase()+s.substr(1)
            sts.push(temp);
        })
        return sts.join("")
    }
}

